<?php


namespace App\Http\Controllers\xcx\worker;

use App\Http\Controllers\common\CommonFun;
use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\BranchUtils;
use App\Http\Controllers\Controller;
use App\Models\WorkerBranchManage;
use App\Models\WorkerBranch;
use Illuminate\Http\Request;
use App\Models\WorkerRole;
use App\Models\Worker;


class WorkerController extends Controller
{
    // 员工扫码入职
    public function WorkerInduction(Request $request) {
        try {
            $all = $request->all();
            $where = [];
            $where['name'] = $all['name'];
            $where['inTime'] = $all['date'];
            $branch_id = $all['branch_id'];
            $where['contactNum'] = $all['tel'];
            $where['phoneNum'] = $all['account'];
            $where['password'] = CommonFun::md5(substr($all['password'], 11, 6));;
            $where['id_type'] = $all['id_type'];
            $where['id_number'] = $all['id_number'];
            $birth = substr($all['id_number'], 6, 8);
            $year = substr($birth, 0, 4);
            $mouth = substr($birth, 4, 2);
            $day = substr($birth, 6, 2);
            $birthday = $year . '-' . $mouth . '-' . $day;      //通过身份证号解析出的生日
            $sex = substr($all['id_number'], 16, 1);          //通过身份证解析出性别
            if ($sex % 2 == 0) {
                $sex = 2;
            } else {
                $sex = 1;
            }
            $where['sex'] = $sex;
            $where['birthday'] = $birthday;
            $where['user_status'] = 2;
            $where['account_status'] = 1;
        } catch (\Throwable $th) {
            return LMReturn::Error('传入参数有误或缺少');
        }
        try {
            $worker = Worker::create($where);
            $worker_branch = [
                "worker_id"=>$worker->id,
                "branch_id"=>$branch_id
            ];
            WorkerBranch::create($worker_branch);
            return LMReturn::Success();
        } catch (\Throwable $th) {
            return LMReturn::Error('添加失败');
        }
    }

    // 入职待审核员工列表
    public function WorkerAuditList(Request $request) {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        $reviewer_id = $tokenInfo['worker_id'];
        $all = $request->all();
        $reviewer_branch = Worker::where('id', $reviewer_id)->with('branchesManage')->first();
        $branch_dics = BranchUtils::getAllBranchs();
        $branch_list = [];
        if (count($reviewer_branch['branchesManage']) > 0) {
            foreach ($reviewer_branch['branchesManage'] as $branchesManage) {
                $manage_branch_id = $branchesManage['id'];
                $branch_list_a = BranchUtils::getChildBranchs($manage_branch_id, $branch_dics, [$manage_branch_id]);
                foreach ($branch_list_a as $a) {
                    if (!in_array($a, $branch_list)) {
                        $branch_list[] = $a;
                    }
                }
            }
        } else {
            return LMReturn::Error('抱歉，您没有权限查看新入职员工');
        }
        $type = $all['type'] ?? 1;
        if ($type == 1) {
            $size = $all['size'] ?? 10;
            $worker_list = Worker::where('user_status', 2)
                ->whereHas('branches', function ($query) use ($branch_list) {
                    $query->whereIn('branch_id', $branch_list);
                })
                ->with('branches')
                ->orderBy('worker.create_time', 'desc')
                ->paginate($size);
            return $worker_list;
        } else {
            if (isset($all['worker_id'])) {
                $worker = Worker::where('id', $all['worker_id'])
                    ->where('user_status', 2)
                    ->whereHas('branches', function ($query) use ($branch_list) {
                        $query->whereIn('branch_id', $branch_list);
                    })
                    ->with('branches')
                    ->first();
                if ($worker) {
                    return LMReturn::Success($worker);
                } else {
                    return LMReturn::Error('您查找的新员工不存在或您没有权限查看此员工');
                }
            } else {
                return LMReturn::Error('未传入要查找的新员工');
            }
        }
    }

    // 员工入职审核
    public function WorkerAudit(Request $request) {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        $reviewer_id = $tokenInfo['worker_id'];
        $all = $request->all();
        if (isset($all['is_pass'])) {
            $is_pass = $all['is_pass'];
        } else {
            return LMReturn::Error('未选择审核结果');
        }
        $reviewer_branch = Worker::where('id', $reviewer_id)->with('branchesManage')->first();
        $branch_dics = BranchUtils::getAllBranchs();
        $branch_list = [];
        if (count($reviewer_branch['branchesManage']) > 0) {
            foreach ($reviewer_branch['branchesManage'] as $branchesManage) {
                $manage_branch_id = $branchesManage['id'];
                $branch_list_a = BranchUtils::getChildBranchs($manage_branch_id, $branch_dics, [$manage_branch_id]);
                foreach ($branch_list_a as $a) {
                    if (!in_array($a, $branch_list)) {
                        $branch_list[] = $a;
                    }
                }
            }
        } else {
            return LMReturn::Error('抱歉，您没有权限查看新入职员工');
        }
        if (isset($all['worker_id'])) {
            $worker = Worker::where('id', $all['worker_id'])
                ->where('user_status', 2)
                ->whereHas('branches', function ($query) use ($branch_list) {
                    $query->whereIn('branch_id', $branch_list);
                })
                ->with('branches')
                ->first();
            if ($worker) {
                if ($is_pass == 1) {
                    if (isset($all['role_id'])) {
                        $role_id = $all['role_id'];
                    } else {
                        return LMReturn::Error('未设置员工角色');
                    }
                    if (isset($all['manage_branch_id_list']) && count($all['manage_branch_id_list']) > 0) {
                        foreach ($all['manage_branch_id_list'] as $manage_branch_id) {
                            $manage_branch['branch_id'] = $manage_branch_id;
                            $manage_branch['worker_id'] = $all['worker_id'];
                            WorkerBranchManage::create($manage_branch);
                        }
                    }
                    WorkerRole::create(['worker_id'=>$all['worker_id'], 'role_id'=>$role_id]);
                    $worker->user_status = 1;
                    $worker->account_status = 2;
                    $worker->save();
                } else {
                    $worker->delete();
                }
                return LMReturn::Success();
            } else {
                return LMReturn::Error('您查找的新员工不存在或您没有权限管理此员工');
            }
        } else {
            return LMReturn::Error('未传入待审核的新员工');
        }
    }

    // 员工转正
    public function WorkerPositive(Request $request) {
        //去验证token值
        $tokenInfo = json_decode($this->verifyTokenLm(), true);
        //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
        if ($tokenInfo['code'] == 400) {
            return LMReturn::Error($tokenInfo['msg'], $tokenInfo['code']);
        }
        $reviewer_id = $tokenInfo['worker_id'];
        $all = $request->all();
        $positive_time = $all['positive_time'] ?? date('Y-m-d', time());
        $reviewer_branch = Worker::where('id', $reviewer_id)->with('branchesManage')->first();
        $branch_dics = BranchUtils::getAllBranchs();
        $branch_list = [];
        if (count($reviewer_branch['branchesManage']) > 0) {
            foreach ($reviewer_branch['branchesManage'] as $branchesManage) {
                $manage_branch_id = $branchesManage['id'];
                $branch_list_a = BranchUtils::getChildBranchs($manage_branch_id, $branch_dics, [$manage_branch_id]);
                foreach ($branch_list_a as $a) {
                    if (!in_array($a, $branch_list)) {
                        $branch_list[] = $a;
                    }
                }
            }
        } else {
            return LMReturn::Error('抱歉，您没有员工转正权限');
        }
        if (isset($all['worker_id'])) {
            $worker = Worker::where('id', $all['worker_id'])
                ->where('user_status', 1)
                ->where('is_positive', 2)
                ->whereHas('branches', function ($query) use ($branch_list) {
                    $query->whereIn('branch_id', $branch_list);
                })
                ->first();
            if ($worker) {
                $worker->is_positive = 1;
                $worker->positive_time = $positive_time;
                $worker->save();
                return LMReturn::Success();
            } else {
                return LMReturn::Error('您查找的员工不存在或您没有权限管理此员工');
            }
        } else {
            return LMReturn::Error('未传入待转正的员工');
        }
    }
}
